<?php
namespace Admin\Model;
use Think\Model;
use Think\Upload;

class ProdetailModel extends Model
{
	protected $_validate = array(
		array('foodname','require','菜品名称不能为空！'),
		array('foodname','','菜品名称不能重复！',1,'unique'),
		array('foodconstruct','require','菜品介绍不能为空！'),
		array('foodprice','require','菜品价格不能为空！'),
	);
	// 菜单的展示
	public function index()
	{

	}
	// 增加菜品方法
	public function saveData($input_data, $file)
	{
		if(!$file['error']){
			$config = array('rootPath' => SERVER_PATH.'/public/upload/images/');
			$upload = new Upload($config);
			$uploadData = $upload -> uploadOne($file);
		}else {
			$this -> error('上传文件类型有错误！', U('admin/product/add'), 2);
		}
		$input_data['foodimg'] = '/public/upload/images/'.$uploadData['savepath'].$uploadData['savename'];
		$addData = array(
						'foodname' => $input_data['foodname'],
						'ordernumber' => $input_data['ordernumber'],
						'foodconstruct' => $input_data['foodconstruct'],
						'foodprice' => $input_data['foodprice'],
						'foodimg' => $input_data['foodimg']//待修改
					);
		return $result = $this -> add($addData);
	}
	// 查询
	public function searchData($inputData)
	{
		return $result = $this -> where("foodname like '%$inputData[foodname]%' and issale = '$inputData[issale]'") -> count();
	}
	// 删除
	public function deleteOne($deleteId)
	{
		return $result = $this -> delete($deleteId);
	}
	// 编辑
	public function edit($editId)
	{
		return $result = $this -> where("id = $editId") -> find();
	}
	// 编辑写入
	public function editResult($inputData, $file)
	{
		// 数据验证
		$verify = $this -> create($inputData);
		if(!$verify) {
			return false;
		}else{
			// 判断是否有需要上传的文件
			if(!$file['error']) {
				$config = array('rootPath' => SERVER_PATH.'/public/upload/images/');
				$upload = new Upload($config);
				$uploadData = $upload -> uploadOne($file);
				if($uploadData){
					$inputData['foodimg'] = '/public/upload/images/'.$uploadData['savepath'].$uploadData['savename'];
				}else{
					return false;
				}
			}
		}
		return $result = $this -> save($inputData);
	}
}